package com.yuyou.fn.timer.task;

import com.github.wens.mybatisplus.examples.Example;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.entity.Period;
import com.yuyou.fn.educational.service.IContinueClassStatService;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.service.IPeriodService;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.service.IAreaTeamService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 *
 * 班续读率
 *
 * Created by wens on 2017/4/19.
 */
@JobHander(value="CalContinueClassStatTask")
@Component
public class CalContinueClassStatTask extends IJobHandler {

    @Resource
    private IPeriodService periodService ;


    @Resource
    private ICourseClassService courseClassService ;

    @Resource
    private IContinueClassStatService continueClassStatService;

    @Resource
    private IAreaTeamService areaTeamService ;


    @Override
    public ReturnT<String> execute(String... params) throws Exception {

        List<AreaTeam> areaTeamList = areaTeamService.findListByExample(AreaTeam.createExample());

        for(AreaTeam areaTeam : areaTeamList ){
            Period currentPeriod = periodService.findCurrentPeriod(areaTeam.getAreaTeamId());
            if(currentPeriod == null ){
                continue;
            }
            Example<CourseClass> example = CourseClass.createExample();
            example.createCriteria().andEqualTo("periodId" , currentPeriod.getPeriodId() ).andIn("status" , Arrays.asList(1,3)) ;
            example.selectProperties("classId");

            List<CourseClass> courseClassList = courseClassService.findListByExample(example);

            for(CourseClass courseClass :  courseClassList ){
                continueClassStatService.updateContinueClassStat(courseClass.getClassId());
            }

        }

        return ReturnT.SUCCESS;
    }

}
